local jianzhenxin = fk.CreateSkill{
    name = "lingling__jianzhenxin",
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
    ["lingling__jianzhenxin"] = "见真心",
    [":lingling__jianzhenxin"] = "每回合每项限一次，当其他角色对你造成伤害后，你可以选择：摸X-1张牌（X为其没有装备牌的装备栏数）；废除其一个有装备牌的装备栏。",
    ["#lingling__jianzhenxin1"] = "摸%arg张牌",
    ["#lingling__jianzhenxin2"] = "废除%dest一个有牌的装备栏",

    ["$lingling__jianzhenxin1"] = "莫说是你，天潢贵胄亦可杀得！",
    ["$lingling__jianzhenxin2"] = "你我不到黄泉，不复相见！",
}

jianzhenxin:addEffect(fk.Damaged, {
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(jianzhenxin.name) and data.from and data.from ~= player and data.from:isAlive()
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices = {}
        if (#table.filter(data.from:getAvailableEquipSlots(),function (subtype)
            return data.from:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(subtype))
        end) - 1) > 0 and not table.contains(player:getTableMark("lingling__jianzhenxin-turn"), "#lingling__jianzhenxin1") then
            table.insert(choices,"#lingling__jianzhenxin1:::"..U.ConvertNumber(#table.filter(data.from:getAvailableEquipSlots(),function (subtype)
                return data.from:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(subtype))
            end) - 1))
        end
        if table.find(data.from:getAvailableEquipSlots(),function (subtype)
            return #data.from:getEquipments(Util.convertSubtypeAndEquipSlot(subtype)) > 0
        end) and not table.contains(player:getTableMark("lingling__jianzhenxin-turn"), "#lingling__jianzhenxin2") then
            table.insert(choices,"#lingling__jianzhenxin2::"..data.from.id)
        end
        if #choices > 0 then
            table.insert(choices, "Cancel")
            local choice = room:askToChoice(player, {
                choices = choices,
                all_choices = {"#lingling__jianzhenxin1:::"..U.ConvertNumber(math.max(#table.filter(data.from:getAvailableEquipSlots(),function (subtype)
                    return data.from:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(subtype))
                end)-1,0)), "#lingling__jianzhenxin2::"..data.from.id, "Cancel"},
                skill_name = jianzhenxin.name,
                prompt = "是否发动 见真心？",
            })
            if choice ~= "Cancel" then
                local anim_type = "drawcard"
                if choice:startsWith("#lingling__jianzhenxin2") then
                    anim_type = "control"
                end
                event:setCostData(self, {choice = choice, anim_type = anim_type})
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local choice = event:getCostData(self).choice
        if choice:startsWith("#lingling__jianzhenxin1") then
            player.room:addTableMarkIfNeed(player, "lingling__jianzhenxin-turn", "#lingling__jianzhenxin1")
            player:drawCards(#table.filter(data.from:getAvailableEquipSlots(),function (subtype)
                return data.from:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(subtype))
            end) - 1, jianzhenxin.name)
        else
            local room = player.room
            room:addTableMarkIfNeed(player, "lingling__jianzhenxin-turn", "#lingling__jianzhenxin2")
            local choices = table.filter(data.from:getAvailableEquipSlots(),function (subtype)
                return #data.from:getEquipments(Util.convertSubtypeAndEquipSlot(subtype)) > 0
            end)
            local slot = room:askToChoice(player, {
                choices = choices,
                skill_name = jianzhenxin.name,
                prompt = "#lingling__jianzhenxin2::"..data.from.id
            })
            room:abortPlayerArea(data.from, slot)
        end
    end,
})

return jianzhenxin